#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

int head[ 10001 ], next[ 10001 ], node[ 10001 ], e;
int n, m;
priority_queue<int> q;

void init( )
{
    int i, u, v;
    e = 0;
    memset( head, -1, sizeof( head ) );
    for ( i = 0; i < n; i++ )
    {
        scanf("%d%d", &v, &u);
        node[ e ] = v; next[ e ] = head[ u ]; head[ u ] = e++;
    }
    while ( !q.empty( ) ) q.pop( );
}

void work( )
{
    int i, j, ans = 0;
    for ( i = 10000; i >= 0; i-- )
    {
        for ( j = head[ i ]; j != -1; j = next[ j ] )
            q.push( node[ j ] );
        for ( j = 0; j < m; j++ )
            if ( !q.empty( ) )
            {
                ans += q.top( );
                q.pop( );
            }
            else
                break;
    }
    printf("%d\n", ans);
}

int main( )
{
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        init( );
        work( );
    }
    return 0;
}
